﻿using System;
using System.Threading.Tasks;

namespace Sidvall.SystemEnvironment.Windows
{
    public class Launcher : ILauncher
    {
        #region Public Members

        #region LaunchFileAsync

        public async Task<bool> LaunchFileAsync(string filePath, LauncherOptions options)
        {
            var processStartInfo = GetProcessStartInfo(filePath, options);
            return await LaunchAsync(processStartInfo);
        }

        #endregion
        #region LaunchFolderAsync

        public async Task<bool> LaunchFolderAsync(string folderPath, FolderLauncherOptions options)
        {
            var processStartInfo = GetProcessStartInfo(folderPath, options);
            return await LaunchAsync(processStartInfo);
        }

        #endregion
        #region LaunchUriAsync

        public async Task<bool> LaunchUriAsync(Uri uri, LauncherOptions options)
        {
            var filePath = uri.ToString();
            return await LaunchFileAsync(filePath, options);
        }

        #endregion

        #region ToString

        public override string ToString()
        {
            return "Launcher - System.Diagnostics.Process";
        }

        #endregion

        #endregion
        #region Private Members

        #region GetProcessStartInfo

        private static System.Diagnostics.ProcessStartInfo GetProcessStartInfo(string path, LauncherOptions options)
        {
            var item = new System.Diagnostics.ProcessStartInfo()
            {
                FileName = path,
            };
            if (options != null)
            {
                item.WindowStyle = GetProcessWindowStyle(options.DesiredRemainingView);
            }
            return item;
        }
        private static System.Diagnostics.ProcessStartInfo GetProcessStartInfo(string path, FolderLauncherOptions options)
        {
            var item = new System.Diagnostics.ProcessStartInfo()
            {
                FileName = path,
            };
            if (options != null)
            {
                item.WindowStyle = GetProcessWindowStyle(options.DesiredRemainingView);
            }
            return item;
        }

        #endregion
        #region GetProcessWindowStyle

        private static System.Diagnostics.ProcessWindowStyle GetProcessWindowStyle(ViewSizePreference style)
        {
            switch (style)
            {
                case ViewSizePreference.UseMore:
                    return System.Diagnostics.ProcessWindowStyle.Maximized;
                case ViewSizePreference.UseMinimum:
                    return System.Diagnostics.ProcessWindowStyle.Minimized;
                case ViewSizePreference.UseNone:
                    return System.Diagnostics.ProcessWindowStyle.Hidden;
                default:
                    return System.Diagnostics.ProcessWindowStyle.Normal;
            }
        }

        #endregion
        #region LaunchAsync

        public Task<bool> LaunchAsync(System.Diagnostics.ProcessStartInfo processStartInfo)
        {
            var item = System.Diagnostics.Process.Start(processStartInfo);
            var process = new Process(item);
            return Task.FromResult(true);
        }

        #endregion

        #endregion
    }
}
